﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.BO.Editoral.Category;
using Portal.User.Security;
using Portal.BO.Editoral.Newslist;
using Portal.Core.DAL;
using System.IO;




namespace Portal.GUI.EditoralOffice.MainOffce.Newslist
{


	public partial class Newslist : System.Web.UI.UserControl
	{
		#region page load  thuc hien phan list cho tung doi tuong
		string strMenuScript = "";
		string scriptString = "<script>{0}</script>";
		protected String Comment_Title = String.Empty;
		protected String Comment_Content = String.Empty;

		protected void Page_Load(object sender, EventArgs e)
		{
			string strcpmode = Request.QueryString["cpmode"].ToString();

			if (!Page.IsPostBack)
			{
				string strUrlRefer = Request.UrlReferrer != null ? Request.UrlReferrer.ToString().ToLower() : "";
				if (strUrlRefer == "" || strUrlRefer.IndexOf("add," + strcpmode) == -1)
				{
					Session.Remove("ddlChuyenmuc");
					Session.Remove("txtFromDate");
					Session.Remove("txtToDate");
					Session.Remove("txtKeyword");
					Session.Remove("cboCategory");
					Session.Remove("cboPage");
					Session.Remove("cboTieuDiem");
					Session.Remove("cboIsHot");
				}
			}

			objListNewsSource.SelectParameters[1].DefaultValue = "false";

			txtFromDate.Attributes.Add("onkeypress", "SetReadOnly()");
			txtToDate.Attributes.Add("onkeypress", "SetReadOnly()");
			txtFromDate.Attributes.Add("onpaste", "return false");
			txtToDate.Attributes.Add("onpaste", "return false");
			txtFromDate.Attributes.Add("oncontextmenu", "return false");
			txtToDate.Attributes.Add("oncontextmenu", "return false");
			if (!IsPostBack)
			{
				Session["cpmode"] = Request.QueryString["cpmode"];
				string strCats = CategoryHelper.Treebuild(cboCategory);
				CategoryHelper.Treebuild(ddlChuyenmuc);

				// Init value
				ddlChuyenmuc.SelectedValue = Session["ddlChuyenmuc"] != null ? Session["ddlChuyenmuc"].ToString() : "0";
				txtToDate.Text = Session["txtToDate"] != null ? Session["txtToDate"].ToString() : "";
				txtFromDate.Text = Session["txtFromDate"] != null ? Session["txtFromDate"].ToString() : "";
				cboCategory.SelectedValue = Session["cboCategory"] != null ? Session["cboCategory"].ToString() : "0";
				txtKeyword.Text = Session["txtKeyword"] != null ? Session["txtKeyword"].ToString() : "";

				// End Init Value

				if (strCats.Trim() == "")
				{
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status = -2 AND Cat_ID = -2";
					tblFilter.Visible = false;
					divMesMain.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					LinkSendAll.Visible = false;
					LinkDelete.Visible = false; ltrsec4.Visible = false;
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					//this.Unload += new EventHandler(Newslist_Unload);
					return;
				}


				excutive(strcpmode, strCats, "");

				// Tu dong phan trang
				//if (Request.Cookies["VCC_AdminCurrentPage"] != null)
				//{
				//    try
				//    {
				//        string str = Request.Cookies["VCC_AdminCurrentPage"].Value;
				//        if (str.StartsWith(Convert.ToString(Session["cpmode"])))
				//        {
				//            int pIndex = Convert.ToInt16(str.Substring(Convert.ToString(Session["cpmode"]).Length + 1));
				//            grdListNews.PageIndex = pIndex;
				//            grdListNews.DataBind();
				//            if (pIndex >= grdListNews.PageCount)
				//            {
				//                grdListNews.PageIndex = pIndex - 1;
				//                grdListNews.DataBind();
				//            }
				//            cboPage.DataBind();
				//            cboPage.SelectedValue = Convert.ToString(pIndex);
				//        }
				//    }
				//    catch (Exception ex) { }
				//}
				//End Tu dong phan trang

				//this.Unload += new EventHandler(Newslist_Unload);



				// Tu dong Search
				if (Session["ddlChuyenmuc"] != null || (Session["txtToDate"] != null && Session["txtFromDate"] != null))
				{
					Filter();
					//return;
				}
				else
					if (Session["cboCategory"] != null || Session["txtKeyword"] != null)
					{
						btnSearch_Click(null, null);
						//return;
					}
				// End Tu dong Search

				//if (Session["cboPage"] != null)
				//{
				//    cboPage.SelectedValue = Session["cboPage"] != null ? Session["cboPage"].ToString() : "0";
				//    grdListNews.PageIndex = Convert.ToInt32(Session["cboPage"]);
				//    objListNewsSource.SelectParameters[1].DefaultValue = "false";
				//}
			}
		}

		void Newslist_Unload(object sender, EventArgs e)
		{
			if (grdListNews.Rows.Count <= 0)
			{
				string strScript = "<script>ShowHide_tblPage_and_tblSearch();</script>";
				HttpContext.Current.Response.Write(strScript);
			}
		}

		/*void Menu_Unload(object sender, EventArgs e)
		{            
			   HttpContext.Current.Response.Write(strMenuScript);            
		}*/

		#endregion


		private void excutive(string strcpmode, string strCats, string strAndCat)
		{
			MainSecurity objSercu = new MainSecurity();
			Permission objPer = objSercu.GetPermission(Page.User.Identity.Name);
			
			if (strCats.Trim() != "")
			{
				strCats = " AND Cat_ID in (" + strCats + ") ";
				strAndCat = strCats + strAndCat;
			}
			switch (strcpmode)
			{
				case "templist":
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=0 " + strAndCat;
					HideGridCol(2);
					HideGridCol(3);
					HideGridCol(4);
					HideGridCol(8);
					HideGridCol(9);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(13);
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					lblLabel.Text = "Danh sách bài viết lưu tạm";
					break;
				case "sendlist":
					lblLabel.Text = "Danh sách bài viết đã gửi chờ biên tập";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=1 " + strAndCat;
					HideGridCol(2);
					HideGridCol(3);
					HideGridCol(4);
					HideGridCol(6);
					HideGridCol(7);
					HideGridCol(8);
					HideGridCol(9);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(12);
					HideGridCol(13);
					HideGridCol(14);
					HideGridCol(15);
					LinkSendAll.Visible = false; Literal0.Visible = false;
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					LinkDelete.Visible = false; ltrsec4.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;

					////grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày gửi";
					btnChonTatCa.Visible = false;
					btnBoChon.Visible = false;
					ltrNganCachBoChon_ChonTatCat.Visible = false;
					grdListNews.Columns[0].Visible = false;
					break;
				case "sendapprovallist":
					lblLabel.Text = "Danh sách bài viết đã gửi chờ duyệt";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=2 " + strAndCat;
					HideGridCol(2);
					HideGridCol(3);
					HideGridCol(4);
					HideGridCol(6);
					HideGridCol(7);
					HideGridCol(8);
					HideGridCol(9);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(12);
					HideGridCol(13);
					HideGridCol(14);
					HideGridCol(15);
					LinkSendAll.Visible = false; Literal0.Visible = false;
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					LinkDelete.Visible = false; ltrsec4.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;

					btnBoChon.Visible = false;
					btnChonTatCa.Visible = false;
					ltrNganCachBoChon_ChonTatCat.Visible = false;
					//grdListNews.Columns[0].Visible = false;
					//grdListNews.Columns[5].Visible = false;
					////grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày gửi";

					break;
				case "dellist":
					lblLabel.Text = "Danh sách bài viết xóa tạm";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=6 " + strAndCat;
					HideGridCol(2);
					HideGridCol(4);
					HideGridCol(3);
					HideGridCol(7);
					HideGridCol(8);
					HideGridCol(9);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(12); //HideGridCol(13);
					LinkSendAll.Visible = false; Literal0.Visible = false;
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					LinkDelete.Visible = false; ltrsec4.Visible = false;
					////grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày xóa";
					break;
				//Cung la waitlist nhung voi quyen khac nhau thi status nhan dc la khac nhau:
				//Neu la BTV thi trang thai la 1 va mode la editwaitlist.
				case "editwaitlist":
					lblLabel.Text = "Danh sách bài viết chờ biên tập";
					//objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=1 AND Cat_ID in (" + strCats + ")" + strAndCat + " AND News_CreateDate > '" + DateTime.Now.ToString("yyyy/MM/dd") + "'";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=1 " + strAndCat + " ";
					HideGridCol(6);
					HideGridCol(4);
					HideGridCol(3);
					HideGridCol(6);
					HideGridCol(9);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(13);
					HideGridCol(15);
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					////grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày gửi";
					break;
				case "editinglist":
					lblLabel.Text = "Danh sách bài viết nhận biên tập";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=1 " + strAndCat;
					HideGridCol(4);
					HideGridCol(3);
					HideGridCol(9);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(13);
					HideGridCol(15);
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					////grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày nhận";
					break;
				case "approvinglist":
					lblLabel.Text = "Danh sách bài viết nhận duyệt";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=2 " + strAndCat;
					HideGridCol(4);
					HideGridCol(3);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(13);
					//HideGridCol(7);
					HideGridCol(15);
					LinkSendAll.Visible = false; Literal0.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					////grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày nhận";


					if (objPer.isXuat_Ban_Bai)
					{
						LinkApproval.Visible = true;
					}
					else
					{
						LinkApproval.Visible = false;
						HideGridCol(9);
					}
					break;
				case "approvalwaitlist":
					lblLabel.Text = "Danh sách bài viết chờ duyệt";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=2 " + strAndCat;
					HideGridCol(4);
					HideGridCol(6);
					HideGridCol(3);
					HideGridCol(10);
					HideGridCol(11);
					HideGridCol(13);
					//HideGridCol(7);
					HideGridCol(15);
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					LinkSendAll.Visible = false; Literal0.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					////grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày gửi";


					if (objPer.isXuat_Ban_Bai)
					{
						LinkApproval.Visible = true;
					}
					else
					{
						LinkApproval.Visible = false;
						HideGridCol(9);
					}

					break;
				case "publishedlist":
					lblLabel.Text = "Danh sách bài viết đã xuất bản";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=3 " + strAndCat;
					HideGridCol(4);
					//HideGridCol(6); 
					HideGridCol(7);
					HideGridCol(8);
					HideGridCol(9);
					HideGridCol(11);
					HideGridCol(12);
					HideGridCol(13);
					LinkSendAll.Visible = false; Literal0.Visible = false;
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					//LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					LinkDelete.Visible = false; ltrsec4.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					//grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày xuất bản";

					//NewsHelper.UpdateCounter();
					//grdListNews.Columns[2].Visible = true;
					grdListNews.Columns[3].Visible = true;
					imgCount.Visible = true;
					break;
				//voi danh sach bai tra lai.
				case "backlist":
					lblLabel.Text = "Danh sách bài viết trả lại";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=5 " + strAndCat;
					HideGridCol(4);
					HideGridCol(3);
					HideGridCol(8);
					HideGridCol(9);
					HideGridCol(10);
					HideGridCol(13);
					HideGridCol(15);
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					//LinkDelete.Visible = false; ltrsec4.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					//grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày gửi";
					grdListNews.Columns[3].Visible = true;
					break;
				//voi danh sach bai tra lai.
				case "removedlist":
					lblLabel.Text = "Danh sách bài viết đã gỡ bỏ";
					objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=7 " + strAndCat;
					HideGridCol(4);
					//HideGridCol(8); 
					HideGridCol(7);
					HideGridCol(10);
					HideGridCol(13);
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDisApproval.Visible = false; ltrsec3.Visible = false;
					//LinkDelete.Visible = false; ltrsec4.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					LinkSendAll.Visible = false; Literal0.Visible = false;
					//grdListNews.Columns[grdListNews.Columns.Count - 3].HeaderText = "Ngày gửi";

					break;
				default:
					//xem quyen cua thang dang set
					MainSecurity objSecu = new MainSecurity();
					Role objrole = objSecu.GetRole(Page.User.Identity.Name);
					if (objrole.isBienTapVien)
					{
						objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=1 " + strAndCat;
						HideGridCol(6);
						HideGridCol(9);
						HideGridCol(10);
						HideGridCol(11);
						HideGridCol(13);
						LinkApproval.Visible = false; ltrsec2.Visible = false;
						LinkDisApproval.Visible = false; ltrsec3.Visible = false;
						lnkRealDel.Visible = false; ltrsec5.Visible = false;

					}
					if (objrole.isPhongVien)
					{
						objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=0 " + strAndCat;
						/*HideGridCol(2);)*/
						HideGridCol(6); HideGridCol(8); HideGridCol(9); HideGridCol(10); HideGridCol(11); HideGridCol(13);
						LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
						LinkApproval.Visible = false; ltrsec2.Visible = false;
						LinkDisApproval.Visible = false; ltrsec3.Visible = false;
						//LinkDelete.Visible = false; ltrsec4.Visible = false;
						lnkRealDel.Visible = false; ltrsec5.Visible = false;

					}
					if (objrole.isThuKyChuyenMuc || objrole.isThuKyToaSoan || objrole.isPhuTrachKenh || objrole.isTongBienTap)
					{
						objListNewsSource.SelectParameters[0].DefaultValue = "News_Status=2 " + strAndCat;
						HideGridCol(6); HideGridCol(10); HideGridCol(11); HideGridCol(13);
						LinkDisApproval.Visible = false; ltrsec3.Visible = false;
						lnkRealDel.Visible = false; ltrsec5.Visible = false;


					}
					break;
				case "mypublished":
					objListNewsSource.SelectParameters[0].DefaultValue = " News_Status=3 " + strAndCat;// +" order by news_createDate";
					objListNewsSource.SelectParameters[2].DefaultValue = HttpContext.Current.User.Identity.Name;
					HideGridCol(6);
					HideGridCol(2);
					//HideGridCol(4); 
					// HideGridCol(6); 
					HideGridCol(7);
					HideGridCol(8);
					HideGridCol(9);
					HideGridCol(11);
					HideGridCol(12);
					HideGridCol(13);
					LinkSendAll.Visible = false; Literal0.Visible = false;
					LinkFeedBackAll.Visible = false; ltrsec1.Visible = false;
					LinkApproval.Visible = false; ltrsec2.Visible = false;
					LinkDelete.Visible = false; ltrsec4.Visible = false;
					lnkRealDel.Visible = false; ltrsec5.Visible = false;
					break;
			}


			//this.Unload +=new EventHandler(Menu_Unload);
			lblScript.Text = String.Format(scriptString, strMenuScript);
		}

		private String GetListNewsName(string strcpmode)
		{
			String ListName = String.Empty;
			switch (strcpmode)
			{
				case "templist":
					//ListName = LogListName.TEMPLIST;
					break;
				case "sendlist":
					//ListName = LogListName.SENDLIST;
					break;
				case "sendapprovallist":
					//ListName = LogListName.SENDAPPROVALLIST;
					break;
				case "dellist":
					//ListName = LogListName.DELLLIST;                    
					break;
				//Cung la waitlist nhung voi quyen khac nhau thi status nhan dc la khac nhau:
				//Neu la BTV thi trang thai la 1 va mode la editwaitlist.
				case "editwaitlist":
					//ListName = LogListName.EDITWAITLIST;
					break;
				case "editinglist":
					//ListName = LogListName.EDITINGLIST;
					break;
				case "approvinglist":
					//ListName = LogListName.APPROVINGLIST;
					break;
				case "approvalwaitlist":
					//ListName = LogListName.APPROVINGWAITINGLIST;
					break;
				case "publishedlist":
					//ListName = LogListName.PUBLISHEDLISH;
					break;
				case "backlist":
					//ListName = LogListName.BACKLIST;
					break;
				case "removedlist":
					//ListName = LogListName.REMOVELIST;
					break;
			}
			return ListName;
		}


		protected void cboPage_SelectedIndexChanged(object sender, EventArgs e)
		{
			//grdListNews.PageIndex = Convert.ToInt32(cboPage.SelectedValue);
			//Session["cboPage"] = cboPage.SelectedValue;
		}

		protected void grdListNews_PageIndexChanged(object sender, EventArgs e)
		{
			grdListNews.SelectedIndex = -1;
		}

		protected void grdListNews_Sorted(object sender, EventArgs e)
		{
			string sortExpression = grdListNews.SortExpression.ToLower();

			switch (sortExpression)
			{
				case "news_title":
					SortColumn(1);
					break;
				case "news_isfocus":
					SortColumn(grdListNews.Columns.Count - 2);
					break;
				case "news_mode":
					SortColumn(grdListNews.Columns.Count - 1);
					break;
				case "viewcount":
					SortColumn(grdListNews.Columns.Count - 3);
					break;
			}
		}
		void SortColumn(int columnIndex)
		{

			string className = grdListNews.SortDirection == SortDirection.Ascending ? "asc" : "desc";
			string tooltip = grdListNews.SortDirection == SortDirection.Ascending ? "Xắp xếp theo thứ tự tăng dần" : "Xắp sếp theo thứ tự giảm dần";

			for (int i = 0; i < grdListNews.Columns.Count; i++)
			{
				grdListNews.Columns[i].HeaderStyle.CssClass = "";
				grdListNews.HeaderRow.Cells[i].ToolTip = "";
			}
			grdListNews.Columns[columnIndex].HeaderStyle.CssClass = className;
			grdListNews.HeaderRow.Cells[columnIndex].ToolTip = tooltip;
		}
		#region Item command
		public void GetComment(Int64 News_ID)
		{
			ActionRow objActionRow = NewslistHelper.getLastestAction(News_ID);
			//openCommentPopup(objActionRow.Comment_Title, objActionRow.Content);
			Comment_Title = objActionRow.Comment_Title;
			Comment_Content = objActionRow.Content;
		}

		protected void grdListNews_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			string strCmd = e.CommandName.ToLower();
			string strcpmode = Request.QueryString["cpmode"].ToString();
			txtCpmode.Value = strcpmode;

			MainSecurity objSecu = new MainSecurity();
			Role objrole = objSecu.GetRole(Page.User.Identity.Name);

			LogRow logRow = new LogRow();
			String action = String.Empty;

			switch (strCmd)
			{

				//trang thai gui bai len - tuy theo truong hop se gan gui len dau?
				case "sendup":
					objListNewsSource.UpdateParameters[0].DefaultValue = e.CommandArgument.ToString();

					if (objrole.isBienTapVien || objrole.isThuKyToaSoan || objrole.isThuKyChuyenMuc)
						objListNewsSource.UpdateParameters[1].DefaultValue = "2";
					else if (objrole.isPhongVien)
						objListNewsSource.UpdateParameters[1].DefaultValue = "1";

					//tin dang o trang thai bi tra lai
					/*if (strcpmode.IndexOf("backlist") >= 0)
					{
						//lay trang thai cuoi khi bi tra de lam trang thai khoi phuc khi duoc gui len. Tra ve dang o tt nao thi gui len o tt do.
						int intLastStatus = Portal.BO.Editoral.Newslist.NewslistHelper.getLastestStatus(Convert.ToInt64(e.CommandArgument.ToString()));
						objListNewsSource.UpdateParameters[1].DefaultValue = "" + intLastStatus;
					}*/

					objListNewsSource.Update();

					//action = Log.GUI_LEN;
					break;
				case "deletetemp":
					//Trong truong hop xoa tam thi status =6 o bat cu truong hop nao va k luu vao action
					objListNewsSource.UpdateParameters[0].DefaultValue = e.CommandArgument.ToString();
					objListNewsSource.UpdateParameters[1].DefaultValue = "6";
					objListNewsSource.Update();
					//action = Log.XOA_TAM;
					break;
				case "delete":
					objListNewsSource.DeleteParameters[0].DefaultValue = e.CommandArgument.ToString();
					objListNewsSource.Delete();
					//action = Log.XOA_BAI;
					break;
				case "approval":
					objListNewsSource.UpdateParameters[0].DefaultValue = e.CommandArgument.ToString();
					objListNewsSource.UpdateParameters[1].DefaultValue = "3";
					objListNewsSource.Update();
					//action = Log.TIEU_DIEM;
					break;
				case "history":
					// Show comment
					Int64 _news_id = Convert.ToInt64(e.CommandArgument.ToString());
					ActionRow objActionRow = NewslistHelper.getLastestAction(_news_id);
					openCommentPopup(objActionRow.Comment_Title, objActionRow.Content);
					break;
				case "disapproval":
					// Go bo tin
					objListNewsSource.UpdateParameters[0].DefaultValue = e.CommandArgument.ToString();
					objListNewsSource.UpdateParameters[1].DefaultValue = "7";
					objListNewsSource.Update();
					//action = Log.KHONG_TIEU_DIEM;
					break;
				case "logNews":
					Response.Redirect("lognewslist/" + e.CommandArgument.ToString() + ".chn");
					break;
				case "sortbynewstype":
					break;
			}

		}
		#endregion

		private void openCommentPopup(string comment_title, string content)
		{
			txtTitle.Value = comment_title;
			txtContent.Value = content;
			Page.ClientScript.RegisterStartupScript(GetType(), "ShowComment", "<script language='javascript'>\n ShowComment() \n </script> ");
		}

		private string getCheckedRow()
		{
			string strDel = Request.Form["chkSelect"] != null ? Request.Form["chkSelect"].ToString() : "";
			return strDel;
		}
		private void HideGridCol(int _i)
		{
			//if (_i <= 6) grdListNews.Columns[_i].Visible = false;
			strMenuScript += "\narr[" + _i.ToString() + "]= false;\n";// String.Format(strMenuScript, _i);                     
		}

		protected void grdListNews_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{

				Literal ltrInfo = e.Row.FindControl("ltrInfo") as Literal;
				CheckBox chkIsFocus = e.Row.FindControl("chkIsFocus") as CheckBox;
				DropDownList cboIsHot = e.Row.FindControl("cboIsHot") as DropDownList;
				


				string strcpmode = Request.QueryString["cpmode"].ToString();
				DataRowView data = e.Row.DataItem as DataRowView;
				switch (strcpmode)
				{
					case "templist":
						ltrInfo.Text = "Lần sửa cuối: {0}";
						ltrInfo.Text = string.Format(ltrInfo.Text, ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					case "sendlist":
						ltrInfo.Text = "Ngày gửi: {0}";
						ltrInfo.Text = string.Format(ltrInfo.Text, ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						chkIsFocus.Enabled = false;
						cboIsHot.Enabled = false;
						break;
					case "sendapprovallist":
						ltrInfo.Text = "Ngày gửi: {0}";
						ltrInfo.Text = string.Format(ltrInfo.Text, ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						chkIsFocus.Enabled = false;
						cboIsHot.Enabled = false;
						break;
					case "dellist":
						ltrInfo.Text = "Người xóa: <b>{0}</b>, Ngày xóa: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					//Cung la waitlist nhung voi quyen khac nhau thi status nhan dc la khac nhau:
					//Neu la BTV thi trang thai la 1 va mode la editwaitlist.
					case "editwaitlist":
						// nguoi dua + ngay gui
						ltrInfo.Text = "Người đưa: <b>{0}</b>, Ngày gửi: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					case "editinglist":
						// nguoi dua + ngay nhan
						ltrInfo.Text = "Người đưa: <b>{0}</b>, Ngày nhận: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					case "approvinglist":
						// nguoi dua + ngay nhan
						ltrInfo.Text = "Người đưa: <b>{0}</b>, Ngày nhận: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					case "approvalwaitlist":
						// nguoi dua + ngay gui
						ltrInfo.Text = "Người đưa: <b>{0}</b>, Ngày gửi: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					case "publishedlist":
						// nguoi dua + lượt xem + ngày xuất bản
						if ((DateTime)data["ModifiedDate"] > DateTime.Now)
							ltrInfo.Text = "Người đưa: <b>{0}</b>, NXB: <span style=\"color:red; text-decoration:underline;\">{1}</span>";
						else
							ltrInfo.Text = "Người đưa: <b>{0}</b>, NXB: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					case "backlist":
						ltrInfo.Text = "Người đưa: <b>{0}</b>, Ngày gửi: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
					case "removedlist":
						// nguoi dua + lượt xem + ngày xuất bản
						ltrInfo.Text = "Người đưa: <b>{0}</b>, Ngày gửi: {1}";
						ltrInfo.Text = string.Format(ltrInfo.Text, data["Sender_ID"].ToString(), ((DateTime)data["ModifiedDate"]).ToString("dd/MM/yyyy hh:mm tt"));
						break;
				}
			}
		}
		protected void LinkSendAll_Click(object sender, EventArgs e)
		{
			MainSecurity objSecu = new MainSecurity();
			Role objrole = objSecu.GetRole(Page.User.Identity.Name);
			string strcpmode = Request.QueryString["cpmode"].ToString();



			// TKTS va TKM thi Gui bai o moi cap se vao luon "Danh sách bài chờ biên tập"
			if (objrole.isThuKyToaSoan || objrole.isThuKyChuyenMuc || objrole.isPhuTrachKenh || objrole.isTongBienTap || objrole.isBienTapVien)
				objListNewsSource.UpdateParameters[1].DefaultValue = "2";
			else if (objrole.isPhongVien)
				objListNewsSource.UpdateParameters[1].DefaultValue = "1";
			// ******* End Add By Tqdat

			string value = objListNewsSource.UpdateParameters[2].DefaultValue = getCheckedRow();
			objListNewsSource.Update();



			//if (strcpmode == "")
			//	Response.Redirect("/office.chn");
			//Response.Redirect("/office/" + strcpmode + ".chn");
		}
		protected void LinkDelete_Click(object sender, EventArgs e)
		{
			string strcpmode = Request.QueryString["cpmode"].ToString();
			objListNewsSource.UpdateParameters[1].DefaultValue = "6";
			String value = objListNewsSource.UpdateParameters[2].DefaultValue = getCheckedRow();
			objListNewsSource.Update();

			#region Log
			//if (!value.Equals(String.Empty))
			//{
			//    LogHelper logHelper = new LogHelper();
			//    String[] News_ID = value.Split(',');
			//    int TotalUpdate = News_ID.Length;

			//    LogRow logRow = new LogRow();
			//    String ListName = GetListNewsName(Request.QueryString["cpmode"].ToString());

			//    for (int i = 0; i < TotalUpdate; i++)
			//    {
			//        logRow.UserName = HttpContext.Current.User.Identity.Name;
			//        logRow.Type = (int)LogType.News;
			//        logRow.News_ID = Convert.ToInt64(News_ID[i]);
			//        logRow.CreatedDate = DateTime.Now;
			//        logRow.Action = ListName + Log.XOA_BAI;
			//        logHelper.Insert(logRow);
			//    }
			//}
			#endregion

			//if (strcpmode == "")
			//    Response.Redirect("/office.chn");
			//Response.Redirect("/office/" + strcpmode + ".chn");
		}
		protected void LinkDisApproval_Click(object sender, EventArgs e)
		{
			// Go bo nhieu tin
			String value = objListNewsSource.UpdateParameters[2].DefaultValue = getCheckedRow();
			objListNewsSource.UpdateParameters[1].DefaultValue = "7";
			objListNewsSource.Update();

			#region Log
			//if (!value.Equals(String.Empty))
			//{
			//    LogHelper logHelper = new LogHelper();
			//    String[] News_ID = value.Split(',');
			//    int TotalUpdate = News_ID.Length;

			//    LogRow logRow = new LogRow();
			//    String ListName = GetListNewsName(Request.QueryString["cpmode"].ToString());

			//    for (int i = 0; i < TotalUpdate; i++)
			//    {
			//        logRow.UserName = HttpContext.Current.User.Identity.Name;
			//        logRow.Type = (int)LogType.News;
			//        logRow.News_ID = Convert.ToInt64(News_ID[i]);
			//        logRow.CreatedDate = DateTime.Now;
			//        logRow.Action = ListName + Log.GO_BO;
			//        logHelper.Insert(logRow);
			//    }
			//}
			#endregion
			//Response.Redirect("/office/" + strcpmode + ".chn");

		}

		protected void btnSearch_Click(object sender, EventArgs e)
		{
			// Reset cac gia tri cua  Filter
			txtFromDate.Text = "";
			txtToDate.Text = "";
			ddlChuyenmuc.SelectedValue = "0";
			Session.Remove("ddlChuyenmuc");
			Session.Remove("txtFromDate");
			Session.Remove("txtToDate");
			// End Reset

			objListNewsSource.SelectParameters[1].DefaultValue = "true";

			grdListNews.SelectedIndex = -1;
			//cboPage.SelectedValue = "0";

			//txtCpmode.Value = "Test Param";
			Portal.BO.SearchHelper objhelp = new Portal.BO.SearchHelper();
			string strcpmode = Request.QueryString["cpmode"].ToString();
			string strKeyword = txtKeyword.Text.Trim().Replace("'", "");
			string[] strKeys = strKeyword.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
			string strWhere = objhelp.getAndCond("News_Title", strKeys);
			if (strWhere.Trim() != "")
				strWhere = " And " + strWhere;

			if (cboTieuDiem.SelectedIndex > 0)
			{
				Session["cboTieuDiem"] = cboTieuDiem.SelectedValue;
				strWhere += " And [News].News_isFocus = " + cboTieuDiem.SelectedValue;
			}
			else
			{
				Session.Remove("cboTieuDiem");
			}

			if (cboIsHot.SelectedIndex > 0)
			{
				Session["cboIsHot"] = cboIsHot.SelectedValue;
				strWhere += " And [News].News_mode = " + cboIsHot.SelectedValue;
			}
			else
				Session.Remove("cboIsHot");

			string strCat = cboCategory.SelectedValue;
			if (strCat == "0")
				strCat = CategoryHelper.Treebuild(cboCategory);

			excutive(strcpmode, strCat, strWhere);
			//objdspage.Update();

			//this.grdListNews.Unload += new EventHandler(grdListNews_Unload);
			// Luu cac gia tri TimKiem
			Session["txtKeyword"] = txtKeyword.Text.Trim();
			Session["cboCategory"] = cboCategory.SelectedValue;

		}

		void grdListNews_Unload(object sender, EventArgs e)
		{
			Session.Remove("ddlChuyenmuc");
			Session.Remove("txtFromDate");
			Session.Remove("txtToDate");
			Session.Remove("txtKeyword");
			Session.Remove("cboCategory");
			Session.Remove("cboTieuDiem");
			Session.Remove("cboIsHot");
		}
		/// <summary>
		/// Ham thuc hien lenh xoa that nhung tin da duoc chon trong hom rac
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void lnkRealDel_Click(object sender, EventArgs e)
		{
			string strcpmode = Request.QueryString["cpmode"].ToString();
			String value = objListNewsSource.DeleteParameters[0].DefaultValue = getCheckedRow();
			objListNewsSource.Delete();

			#region Log
			//if (!value.Equals(String.Empty))
			//{
			//    LogHelper logHelper = new LogHelper();
			//    String[] News_ID = value.Split(',');
			//    int TotalUpdate = News_ID.Length;

			//    LogRow logRow = new LogRow();
			//    String ListName = GetListNewsName(Request.QueryString["cpmode"].ToString());

			//    for (int i = 0; i < TotalUpdate; i++)
			//    {
			//        logRow.UserName = HttpContext.Current.User.Identity.Name;
			//        logRow.Type = (int)LogType.News;
			//        logRow.News_ID = Convert.ToInt64(News_ID[i]);
			//        logRow.CreatedDate = DateTime.Now;
			//        logRow.Action = ListName + Log.XOA_BAI;
			//        logHelper.Insert(logRow);
			//    }
			//}
			#endregion

			//if (strcpmode == "")
			//    Response.Redirect("/office.chn");
			//Response.Redirect("/office/" + strcpmode + ".chn");
		}
		protected void LinkFeedBackAll_Click(object sender, EventArgs e)
		{
			string strcpmode = Request.QueryString["cpmode"].ToString();
			objListNewsSource.UpdateParameters[1].DefaultValue = "5";
			String value = objListNewsSource.UpdateParameters[2].DefaultValue = getCheckedRow();
			objListNewsSource.Update();

			#region Log
			//if (!value.Equals(String.Empty))
			//{
			//    LogHelper logHelper = new LogHelper();
			//    String[] News_ID = value.Split(',');
			//    int TotalUpdate = News_ID.Length;

			//    LogRow logRow = new LogRow();
			//    String ListName = GetListNewsName(Request.QueryString["cpmode"].ToString());

			//    for (int i = 0; i < TotalUpdate; i++)
			//    {
			//        logRow.UserName = HttpContext.Current.User.Identity.Name;
			//        logRow.Type = (int)LogType.News;
			//        logRow.News_ID = Convert.ToInt64(News_ID[i]);
			//        logRow.CreatedDate = DateTime.Now;
			//        logRow.Action = ListName + Log.TRA_TIN;
			//        logHelper.Insert(logRow);
			//    }
			//}
			#endregion

			//if (strcpmode == "")
			//    Response.Redirect("/office.chn");
			//Response.Redirect("/office/" + strcpmode + ".chn");
		}

		protected void LinkApproval_Click(object sender, EventArgs e)
		{
			string strcpmode = Request.QueryString["cpmode"].ToString();
			objListNewsSource.UpdateParameters[1].DefaultValue = "3";
			String value = objListNewsSource.UpdateParameters[2].DefaultValue = getCheckedRow();
			objListNewsSource.Update();

			#region Log
			//if (!value.Equals(String.Empty))
			//{
			//    LogHelper logHelper = new LogHelper();
			//    String[] News_ID = value.Split(',');
			//    int TotalUpdate = News_ID.Length;

			//    LogRow logRow = new LogRow();
			//    String ListName = GetListNewsName(Request.QueryString["cpmode"].ToString());

			//    for (int i = 0; i < TotalUpdate; i++)
			//    {
			//        logRow.UserName = HttpContext.Current.User.Identity.Name;
			//        logRow.Type = (int)LogType.News;
			//        logRow.News_ID = Convert.ToInt64(News_ID[i]);
			//        logRow.CreatedDate = DateTime.Now;
			//        logRow.Action = ListName + Log.XUAT_BAN;
			//        logHelper.Insert(logRow);
			//    }
			//}
			#endregion

			//if (strcpmode == "")
			//    Response.Redirect("/office.chn");
			//Response.Redirect("/office/" + strcpmode + ".chn");
		}

		private void DisableAll()
		{

			LinkSendAll.Visible = false;
			LinkDisApproval.Visible = false;
			lnkRealDel.Visible = false;

			//lblNumPageView.Visible = false;
			//lblNumPageView.Text = "";

			//cboPage.Visible = false;
			this.LinkApproval.Visible = false;
			this.LinkDelete.Visible = false;
			this.LinkDisApproval.Visible = false;
			this.LinkFeedBackAll.Visible = false;
			Literal0.Visible = false;
			ltrsec1.Visible = false;
			ltrsec2.Visible = false;
			ltrsec3.Visible = false;
			ltrsec4.Visible = false;
			ltrsec5.Visible = false;

			tblSearch.Visible = false;

		}

		protected void grdListNews_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			GridViewRow grdrow = this.grdListNews.Rows[e.RowIndex];
			LogRow logRow = new LogRow();
			String action = String.Empty;

			if (grdrow != null)
			{
				ImageButton btnUpdate = (ImageButton)grdrow.FindControl("btnUpdate");
				if (btnUpdate != null)
					objListNewsSource.UpdateParameters[0].DefaultValue = btnUpdate.CommandArgument.ToString();
				CheckBox cbo = (CheckBox)grdrow.FindControl("chkIsFocus");
				if (cbo != null)
					objListNewsSource.UpdateParameters[3].DefaultValue = cbo.Checked.ToString(); //is focus
				DropDownList cboIsHot = (DropDownList)grdrow.FindControl("cboIsHot");
				if (cboIsHot != null)
					objListNewsSource.UpdateParameters[4].DefaultValue = cboIsHot.SelectedValue.ToString(); //new mode

				objListNewsSource.UpdateParameters[5].DefaultValue = "true";
				//objListNewsSource.Update();
				//TieuDiem

				//if (cbo != null)
				//{
				//    if (cbo.Checked)
				//    {
				//        action = Log.TIEU_DIEM;
				//    }
				//    else
				//    {
				//        action = Log.KHONG_TIEU_DIEM;
				//    }
				//}
				//if (cboIsHot != null)
				//{
				//    if (cboIsHot.SelectedIndex.Equals(0))
				//    {
				//        action = action + Log.THONG_THUONG;
				//    }
				//    else if (cboIsHot.SelectedIndex.Equals(1))
				//    {
				//        action = action + Log.NOI_BAT;
				//    }
				//    else
				//    {
				//        action = action + Log.NOI_BAT_MUC;
				//    }
				//}

				////Log Thông tin cập nhật bài viết vào CSDL
				//action = GetListNewsName(Request.QueryString["cpmode"].ToString()) + action;
				//logRow.UserName = HttpContext.Current.User.Identity.Name;
				//logRow.News_ID = Convert.ToInt64(btnUpdate.CommandArgument);
				//logRow.Type = (int)LogType.News;
				//logRow.Action = action;
				//logRow.CreatedDate = DateTime.Now;
				//LogHelper logHelper = new LogHelper();                
				//logHelper.Insert(logRow);


			}
		}

		protected void btnFilter_Click(object sender, EventArgs e)
		{
			Filter();

			//this.grdListNews.Unload += new EventHandler(grdListNews_Unload);
		}


		private void Filter()
		{
			// Reset cac gia tri cua hop TimKiem
			txtKeyword.Text = "";
			cboCategory.SelectedValue = "0";
			Session.Remove("txtKeyword");
			Session.Remove("cboCategory");
			Session.Remove("cboTieuDiem");
			Session.Remove("cboIsHot");
			// End Reset

			objListNewsSource.SelectParameters[1].DefaultValue = "true";
			//cboPage.SelectedValue = "0";

			string strCat = "";
			string strcpmode = Request.QueryString["cpmode"].ToString();
			string strWhere = "";

			if (ddlChuyenmuc.SelectedValue != "0" && ddlChuyenmuc.SelectedValue != "")
			{
				strCat = ddlChuyenmuc.SelectedValue;
			}
			if (strCat == "" || strCat == "0")
			{
				strCat = CategoryHelper.Treebuild(cboCategory);
			}

			if (txtFromDate.Text.Trim() != "" && txtToDate.Text.Trim() != "")
			{
				string sFromDate = txtFromDate.Text;
				if (sFromDate.Trim() != "")
					sFromDate = sFromDate.Substring(3, 2) + "/" + sFromDate.Substring(0, 2) + "/" + sFromDate.Substring(6, 4);

				string sToDate = txtToDate.Text;
				if (sToDate.Trim() != "")
					sToDate = sToDate.Substring(3, 2) + "/" + sToDate.Substring(0, 2) + "/" + sToDate.Substring(6, 4) + " 23:59.999";

				if (strcpmode.ToLower().IndexOf("publishedlist") >= 0)
					strWhere += " AND News_PublishDate Between '" + sFromDate + "' AND '" + sToDate + "'";
				else
					strWhere += " AND News_ModifiedDate Between '" + sFromDate + "' AND '" + sToDate + "'";
			}

			excutive(strcpmode, strCat, strWhere);
			//objdspage.Update();


			// Luu cac gia tri Filter
			Session["ddlChuyenmuc"] = ddlChuyenmuc.SelectedValue;
			Session["txtFromDate"] = txtFromDate.Text.Trim();
			Session["txtToDate"] = txtToDate.Text.Trim();
			// End Luu cac gia tri filter
		}


		protected void ddlChuyenmuc_SelectedIndexChanged(object sender, EventArgs e)
		{
			Filter();
			//this.grdListNews.Unload += new EventHandler(grdListNews_Unload);
		}

		protected void btnSetTieuDiem_Click(object sender, EventArgs e)
		{
			string arg = hdArgs.Value;
			long newsID = long.Parse(arg.Split(",".ToCharArray())[0]);
			bool tieudiem = bool.Parse(arg.Split(",".ToCharArray())[1]);
			ChannelVN.CoreBO.NewsEditHelper.SetTieuDiem(newsID, tieudiem);
		}

		protected void btnSetLoaiTin_Click(object sender, EventArgs e)
		{
			string arg = hdArgs.Value;
			long newsID = long.Parse(arg.Split(",".ToCharArray())[0]);
			int loaitin = int.Parse(arg.Split(",".ToCharArray())[1]);
			ChannelVN.CoreBO.NewsEditHelper.SetLoaiTin(newsID, loaitin);
		}

	}
}